import java.io.*;
import java.util.Scanner;

public class ConfigReader
{	
	int numPeers = 0, numPreferredNeighbors, unchokingInt, optimisticUnchokingInt, fileSize, pieceSize;
	String fileName;
	int[] peerID, listeningPort, hasFileOrNot;
	String[] hostName;
	
	// read "Common.cfg" and set the relevant variables
	public void readCommon()
	{
		try 
		{
			// create the scanner to read the Common.cfg file
			Scanner scan = new Scanner(new File("Common.cfg"));
		
			scan.next();
			numPreferredNeighbors = scan.nextInt();
			
			scan.next();
			unchokingInt = scan.nextInt();
		
			scan.next();
			optimisticUnchokingInt = scan.nextInt();
		
			scan.next();
			fileName = scan.next();
			
			scan.next();
			fileSize = scan.nextInt();
			
			scan.next();
			pieceSize = scan.nextInt();
			
			// close the scanner
			scan.close();	
		}	
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
	}
	
	// read "PeerInfo.cfg" and set the relevant variables
	public void readPeerInfo()
	{
		try 
		{
			// create the scanner to read the PeerInfo.cfg file
			Scanner scan = new Scanner(new File("PeerInfo.cfg"));
		
			// get the number of peers by counting the number of lines in the PeerInfo.cfg file
			while(scan.hasNextLine())
			{
				numPeers++;
				scan.nextLine();
			}
			
			//System.out.println("Number of peers: " + numPeers);
			
			// close the scanner
			scan.close();
			
			// reset the scanner to the beginning of the file
			scan = new Scanner(new File("PeerInfo.cfg"));
			
			// initialize the arrays that hold the peer info
			peerID = new int[numPeers];
			hostName = new String[numPeers];
			listeningPort = new int[numPeers];
			hasFileOrNot = new int[numPeers];
			
			// fill in the arrays
			for(int i = 0; i < numPeers; i++)
			{
				peerID[i] = scan.nextInt();
				hostName[i] = scan.next();
				listeningPort[i] = scan.nextInt();
				hasFileOrNot[i] = scan.nextInt();
			}
			
			// close the scanner
			scan.close();	
		}	
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
	}
}
